import pkgutil
import importlib
from fastapi import FastAPI, APIRouter
from . import v1

register_packages = [v1]


def register_routers(app: FastAPI):
    routers = []
    for package in register_packages:
        for module_info in pkgutil.iter_modules(package.__path__):
            if module_info.ispkg:
                continue

            module_name = f"{package.__name__}.{module_info.name}"
            module = importlib.import_module(module_name)
            router = getattr(module, "router", None)
            if router and isinstance(router, APIRouter):
                routers.append(router)

    for router in routers:
        app.include_router(router)
